<?php
namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT);

use app\admin\Controller;
use think\Db;
class Brand extends Controller
{
    use \app\admin\traits\controller\Controller;
    // 方法黑名单
    protected static $blacklist = [];

    protected function filter(&$map)
    {
        if ($this->request->param("bname")) {
            $map['bname'] = ["like", "%" . $this->request->param("bname") . "%"];
        }
        if ($this->request->param("content")) {
            $map['content'] = ["like", "%" . $this->request->param("content") . "%"];
        }
        if ($this->request->param("comment")) {
            $map['comment'] = ["like", "%" . $this->request->param("comment") . "%"];
        }
        if ($this->request->param("cid")) {
            $map['c.pid'] = $this->request->param("cid");
        }        
    }
    public function index(){
        $model =  $this->getModel();
        // 列表过滤器，生成查询Map对象
        $map = $this->search($model, [$this->fieldIsDelete => $this::$isdelete]);
        unset($map['cid']);
        // 特殊过滤器，后缀是方法名的
        $actionFilter = 'filter' . $this->request->action();
        if (method_exists($this, $actionFilter)) {
            $this->$actionFilter($map);
        }
        // 自定义过滤器
        if (method_exists($this, 'filter')) {
            $this->filter($map);
        }
        $list=Db::table('tp_brand')->alias('b')
                                            ->join('class c','b.cid=c.id')
                                            ->field('b.*,c.name cname')
                                            ->where($map)
                                            ->order('id desc')
                                            ->paginate(10,false, ['query' => $this->request->get()]);
        $page=$list->render();
        $this->view->assign('list',$list);
        $this->view->assign('page',$page);
        $this->view->assign('count',$list->total());
        return $this->view->fetch();
    }
    public function add(){
        if($this->request->isAjax()){

            $post=$this->request->except(['id']);
            $cid=$post['cid'];
            $post['cid']=$post['c_id'];
            unset($post['c_id']);
            $bname=$post['bname']; //查询重复
            $count=Db::name('brand')->where('bname',$bname)->count();
            if($count){
                //返回错误已存在
                return ajax_return_adv_error('添加的数据已存在，不能重复添加'); 
            }
            Db::startTrans(); 
                
            try {
                Db::name('brand')->insert($post);
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return ajax_return_adv_error($e->getMessage());
            }
            return ajax_return_adv('添加成功');
                    

        }else{
            $cid=$this->request->param('cid'); 
            $class_list=Db::name('class')->where('pid',$cid)->select();
            $this->view->assign('class',$class_list);
        }

        return $this->view->fetch('edit');
    }

    public function edit(){
        if($this->request->isAjax()){
            $post=$this->request->param();
            $cid=$post['cid'];
            $post['cid']=$post['c_id'];
            unset($post['c_id']);
            Db::startTrans(); 
            try {
                Db::name('brand')->update($post);
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return ajax_return_adv_error($e->getMessage());
            }
            return ajax_return_adv('修改成功');
                    
        }else{
            $cid=$this->request->param('cid'); 
            $class_list=Db::name('class')->where('pid',$cid)->select();
            $this->view->assign('class',$class_list);
            $ret=Db::name('brand')->find($this->request->param('id'));
            $this->view->assign('vo',$ret);
            return $this->view->fetch();
        }  
    }

    public function manger(){
        if($this->request->param('id')){
            $res=Db::name('class')->find($this->request->param('id'));
            $this->view->assign('vo',$res);
        }
        $arr=DB::name('class')->order('id desc')->where('pid',$this->request->param('cid'))->paginate(8);
        $page = $arr->render();
        // 模板变量赋值
        $this->view->assign('count',$arr->total());
        $this->view->assign('page', $page);        
        $this->view->assign('list',$arr);
        return $this->view->fetch();
    }
    public function edit_class(){
        if($this->request->post('id')){
           //修改 
            if(Db::name('class')->update($this->request->post())){
                return ajax_return_adv('操作成功'); 
            }else{
                return ajax_return_adv_error('操作失败,请及时联系开发人员'); 
            }
        }else{
            //提交
            $post=$this->request->except('id');
            $res=Db::name('class')->insert($post);                    
            if($res){
                return ajax_return_adv('操作成功'); 
            }else{
                return ajax_return_adv_error('操作失败,请及时联系开发人员'); 
            }
        }
    }

    public function deleteClass(){
        $where=[
            'id'=>$this->request->param('id')
        ];
        $count=Db::name('brand')->where('cid',$this->request->param('id'))->count();

        if($count){
             return ajax_return_adv_error('该分类下有公司存在，不能删除');
        }
        if (false === Db::name('class')->where($where)->delete()) {
            return ajax_return_adv_error($model->getError());
        }

        return ajax_return_adv("删除成功");
    }
    
}
